/**
 * 
 */
package com.eshopper.product.repository.solr;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.PartialUpdate;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.stereotype.Repository;

import com.eshopper.product.document.ProductDocument;
import com.eshopper.product.model.Product;

/**
 * @author Arun Kumar, Nov 20, 2014
 *
 */
@Repository
public class ProductDocumentRepositoryImpl  implements CustomProductDocumentRepository {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductDocumentRepositoryImpl.class);

    @Resource
    private SolrTemplate solrTemplate;

	/* (non-Javadoc)
	 * @see com.eshopper.product.solr.CustomProductDocumentRepository#search(java.lang.String)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<ProductDocument> search(String searchTerm) {

		LOGGER.info("Building a criteria query with search term: {}",
				searchTerm);
		String[] words = searchTerm.split(" ");

		Criteria conditions = createSearchConditions(words);
		SimpleQuery search = new SimpleQuery(conditions);
		search.addSort(sortByIdDesc());

		Page results = solrTemplate.queryForPage(search, ProductDocument.class);
		return results.getContent();

	}
	
    private Criteria createSearchConditions(String[] words) {
        Criteria conditions = null;

        for (String word: words) {
            if (conditions == null) {
                conditions = new Criteria(ProductDocument.FIELD_NAME).contains(word)
                        .or(new Criteria(ProductDocument.FIELD_PRICE).contains(word));
            }
            else {
                conditions = conditions.or(new Criteria(ProductDocument.FIELD_NAME).contains(word))
                        .or(new Criteria(ProductDocument.FIELD_PRICE).contains(word));
            }
        }

        return conditions;
    }

    private Sort sortByIdDesc() {
        return new Sort(Sort.Direction.DESC, ProductDocument.FIELD_ID);
    }

	/* (non-Javadoc)
	 * @see com.eshopper.product.solr.CustomProductDocumentRepository#update(com.eshopper.product.model.Product)
	 */
	@Override
	public void update(Product productEntry) {
		LOGGER.info("Performing partial update for Product entry: {}", productEntry);

        PartialUpdate update = new PartialUpdate(ProductDocument.FIELD_ID, productEntry.getProductId().toString());

        update.add(ProductDocument.FIELD_NAME, productEntry.getProductName());
        update.add(ProductDocument.FIELD_PRICE, productEntry.getPrice());

        solrTemplate.saveBean(update);
        solrTemplate.commit();
		
	}
	
}
